#!/bin/bash

# This shell-script will install the latest version of
# nfcollector to your system & has some system requirements including
# docker, docker-compose, wget. If not satisfied, will help you install
# them using a guide printed in the terminal
# Check more about it's usage at: https://github.com/javadmohebbi/goNfCollector

# The version: v0.0.1-30b
# BETA VERSION
# Author: M. Javad Mohebi
# Github: https://github.com/javadmohebbi
# Dockerhub: https://hub.docker.com/u/javadmohebbi
# Website: https://openintelligence24.com

# change the color of echo output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

COMPOSE_PROJECT_NAME="oi24"

# RANDOM QUERY STRING FOR PREVENTING CACHE
RAND_STR=$(date | md5sum | cut -d" " -f1)

## This VARIABLES are exported because of
## nfupdater commad. This command needs some of them
## Don't panic, this env vars are temparary & will no longer exist if
## you re-open the Terminal
## ;-)

# PROJECT DIR
export PROJECT_DIR=$HOME/oi24/nfcollector

# USER ID
export USER_ID=$(id -u)

# Listen address
export NFC_LISTEN_ADDRESS="0.0.0.0"

# total number of used cpu
export NFC_CPU_NUM="0"

export NFC_LISTEN_PORT="6859"
export NFC_INFLUXDB_HOST="influxdb"
export NFC_INFLUXDB_PORT="8086"
export NFC_INFLUXDB_TOKEN="5vqt0q0b4g_lZwNgp7-8GgPq5Nxf3YY37xbVZP_ypeK_G3dwdNlTrAkcKN_Q6QzbmG-Th96lT_65Kp0j2UD1HA=="
export NFC_INFLUXDB_BUCKET="nfCollector"
export NFC_INFLUXDB_ORG="OPENINTELLIGENCE"
export NFC_IP_REPTATION_IPSUM="$PROJECT_DIR/vendors/ipsum/ipsum.txt"
# added in v0.0.1-30b for replacing config in dockerize deployment automaticaly
export REPLACING_NFC_IP_REPTATION_IPSUM="/opt/nfcollector/vendors/ipsum/ipsum.txt"

export NFC_IP2L_ASN="$PROJECT_DIR/vendors/ip2location/db/IP2LOCATION-LITE-ASN.IPV6.CSV/IP2LOCATION-LITE-ASN.IPV6.CSV"
# added in v0.0.1-30b for replacing config in dockerize deployment automaticaly
export REPLACING_NFC_IP2L_ASN="/opt/nfcollector/vendors/ip2location/db/IP2LOCATION-LITE-ASN.IPV6.CSV/IP2LOCATION-LITE-ASN.IPV6.CSV"

export NFC_IP2L_IP="$PROJECT_DIR/vendors/ip2location/db/IP2LOCATION-LITE-DB11.IPV6.BIN/IP2LOCATION-LITE-DB11.IPV6.BIN"
# added in v0.0.1-30b for replacing config in dockerize deployment automaticaly
export REPLACING_NFC_IP2L_IP="/opt/nfcollector/vendors/ip2location/db/IP2LOCATION-LITE-DB11.IPV6.BIN/IP2LOCATION-LITE-DB11.IPV6.BIN"

export NFC_IP2L_PROXY="$PROJECT_DIR/vendors/ip2location/db/IP2PROXY-LITE-PX10.IPV6.CSV/IP2PROXY-LITE-PX10.IPV6.CSV"
# added in v0.0.1-30b for replacing config in dockerize deployment automaticaly
export REPLACING_NFC_IP2L_PROXY="/opt/nfcollector/vendors/ip2location/db/IP2PROXY-LITE-PX10.IPV6.CSV/IP2PROXY-LITE-PX10.IPV6.CSV"

export NFC_IP2L_LOCAL="$PROJECT_DIR/vendors/ip2location/local-db/local.csv"
# added in v0.0.1-30b for replacing config in dockerize deployment automaticaly
export REPLACING_NFC_IP2L_LOCAL="/opt/nfcollector/vendors/ip2location/local-db/local.csv"

# InfluxDB DIR
export INFLUX_DIR=$PROJECT_DIR/vendors/influxdb

# Grafana DIR
export GRAFANA_DIR=$PROJECT_DIR/vendors/grafana

# error check and stop on error
check_errors() {
    e=$?
    if [ "${e}" -ne "0" ]; then
        echo -e "${RED} [ERROR] Could not continue due to error. Check the above information to solve the issue".
        exit 1
    fi
}

# downlaod latest version of binnary amd64
echo ""
echo -e "${YELLOW} Downloading required files...${NC}"

# download nfcollector
wget -O $PROJECT_DIR/bin/nfcollector https://download.openintelligence24.com/nf/bin/nfcollector?rnd=$RAND_STR
check_errors
chmod +x $PROJECT_DIR/bin/nfcollector
check_errors

# download nfupdater
wget -O $PROJECT_DIR/bin/nfupdater https://download.openintelligence24.com/nf/bin/nfupdater?rnd=$RAND_STR
check_errors
chmod +x $PROJECT_DIR/bin/nfupdater
check_errors

# download databases
$PROJECT_DIR/bin/nfupdater -ipsum -ip2l -ip2l-asn -ip2l-proxy

# download grafana dashboards & conf
wget -O /tmp/grafana.tar.gz https://download.openintelligence24.com/vendors/grafana/grafana.tar.gz?rnd=$RAND_STR && tar -vxf /tmp/grafana.tar.gz -C $GRAFANA_DIR/
check_errors
# chmod +r $GRAFANA_DIR/ -Rv
echo -e "${YELLOW} Chaning permissions & owner of grafana directory. Maybe you need to enter 'sudo' password ${NC}"
sudo chmod -Rv a+w $GRAFANA_DIR/
check_errors
sudo chown $USER:root $GRAFANA_DIR/ -Rv
check_errors

wget -O ./docker-compose.yml https://download.openintelligence24.com/vendors/docker-compose/docker-compose.yml?rnd=$RAND_STR
check_errors

# DOWNLAOD TEMPLATES
wget -O $PROJECT_DIR/etc/collector.yml https://download.openintelligence24.com/nf/etc/nfcol-bash.yml?rnd=$RAND_STR
check_errors
wget -O $PROJECT_DIR/etc/ip2location.yml https://download.openintelligence24.com/nf/etc/nfloc-bash.yml?rnd=$RAND_STR
check_errors

# added in version 0.0.1-30b
wget -O $PROJECT_DIR/etc/trans.yml https://download.openintelligence24.com/nf/etc/trans.yml?rnd=$RAND_STR
check_errors

echo "COMPOSE_PROJECT_NAME=${COMPOSE_PROJECT_NAME}" >$PROJECT_DIR/.env

echo -e "${GREEN}...done!${NC}"
